package com.esms.struts.service;

import java.security.MessageDigest;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.esms.bean.Person;
import com.esms.bean.PersonDAO;
//import com.sun.org.apache.xml.internal.security.utils.Base64;

public class LoginService {
	public static Logger log = Logger.getLogger(LoginService.class);
	private LoginService() {}
	public static LoginService $ = new LoginService(); 
	
	public boolean isLegalUser(Person login, Person result) throws Exception {
		//encrypt the password user input, save to db,
		//when user login , encrypt the password and compare with the db data
		
		//MessageDigest messageDigest = MessageDigest.getInstance("MD5");
		//messageDigest.update(login.getPassword().getBytes());
		//String encryptPassword = new String(messageDigest.digest());�
		//String digestedPwdString = new String(Base64.encode(messageDigest.digest()));
		
		return login.getPassword().equals(result.getPassword()) ? true : false;
	}
	
	public Person getPerson(String userid) throws SQLException {
		List personList = PersonDAO.$.findByUserid(userid);
		if(personList.size() == 0)
			return null;
		else 
			return (Person) personList.get(0);
	}
}
